package org.example.controller;

import org.springframework.web.bind.annotation.*;
import java.util.*;

@RestController
@RequestMapping("/shoppingCart")
@CrossOrigin(origins = "*")
public class ShoppingCartController {

    @GetMapping("/list")
    public Map<String, Object> getCartList(@RequestParam(required = false) Map<String, Object> params) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 1);
        result.put("msg", "查询成功");
        
        // 模拟购物车数据（空购物车）
        List<Map<String, Object>> cartItems = new ArrayList<>();
        result.put("data", cartItems);
        
        return result;
    }

    @PostMapping("/add")
    public Map<String, Object> addToCart(@RequestBody Map<String, Object> cartItem) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 1);
        result.put("msg", "添加成功");
        
        // 模拟返回购物车项
        Map<String, Object> data = new HashMap<>();
        data.put("id", System.currentTimeMillis());
        data.put("name", cartItem.get("name"));
        data.put("image", cartItem.get("image"));
        data.put("amount", cartItem.get("amount"));
        data.put("number", 1);
        data.put("dishId", cartItem.get("dishId"));
        data.put("setmealId", cartItem.get("setmealId"));
        data.put("dishFlavor", cartItem.get("dishFlavor"));
        
        result.put("data", data);
        return result;
    }

    @PostMapping("/sub")
    public Map<String, Object> subFromCart(@RequestBody Map<String, Object> cartItem) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 1);
        result.put("msg", "操作成功");
        
        // 模拟返回购物车项
        Map<String, Object> data = new HashMap<>();
        data.put("id", System.currentTimeMillis());
        data.put("number", 0); // 减少到0
        result.put("data", data);
        
        return result;
    }

    @DeleteMapping("/clean")
    public Map<String, Object> clearCart() {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 1);
        result.put("msg", "清空成功");
        return result;
    }
}

